﻿using LearnWPF.Controls.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LearnWPF.Controls
{
    /// <summary>
    /// AdornedText.xaml 的交互逻辑
    /// </summary>
    public partial class AdornedTextBox : UserControl
    {
        public AdornedTextBox()
        {
            InitializeComponent();
            Loaded += AdornedText_Loaded;
        }

        private void AdornedText_Loaded(object sender, RoutedEventArgs e)
        {
            textbox_TextChanged(null, null);
        }

        private void textbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            int TextLength = textbox.Text.Trim().Length;
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(textbox);

            if (adornerLayer != null)
            {
                if (TextLength == 0)
                {
                    TextBoxAdorner _adorner = new TextBoxAdorner(textbox, "Searching");
                    adornerLayer.Add(_adorner);
                }
                else
                {
                    Adorner[] adorners = adornerLayer.GetAdorners(textbox);
                    if (adorners != null)
                    {
                        foreach (var adorner in adorners)
                        {
                            adornerLayer.Remove(adorner);
                        }
                    }
                }
            }
        }
    }
}
